﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Natol.MSSyncFxProviders.AmazonS3.SampleConsoleApp
{
    public class ConsoleHelper
    {
        public static void WriteLineBreak()
        {
            Console.WriteLine("-----------------------------------------------------");
        }
      
        public static bool GetDebugOptions()
        {
            return AskYesNoQuestion("Write out status of each action?");
        }
        public static int AskForInteger(string message)
        {
            int tryParse;
            if (Int32.TryParse(AskForInfo(message), out tryParse))
            {
                return tryParse;
            }
            else
            {
                Console.WriteLine("Error parsing text. Please enter a valid Integer.");
                //recursive
                return AskForInteger(message);
            }
        }
        public static String AskForInfo(string message)
        {
            Console.WriteLine();
            Console.Write(message);
            string result = Console.ReadLine();
            return result;
        }
        public static bool AskYesNoQuestion(string message)
        {
            message = String.Concat(message, " Press Y/N:");
            var key = AnyKey(message, false);
            return (key.Key == ConsoleKey.Y);
        }

        public static ConsoleKeyInfo AnyKey(string message, bool showKey)
        {
            Console.Write(message);
            var key = Console.ReadKey(true);
            Console.WriteLine(); return key;
        }

        /// <summary>
        /// Asks user for a file system path which exists. Checks path exists, and offers to create if not.
        /// </summary>
        /// <param name="message">Message to present to user before allowing a path to be input.</param>
        /// <param name="rooted">Only allow paths that are rooted (Path.IsPathRooted) as valid.</param>
        /// <returns></returns>
        public static DirectoryInfo AskForValidFileSystemPath(string message, bool rooted)
        {
            //our intended final value
            DirectoryInfo destinationRootPath = null;
            do
            {
                //ask the user for a path
                string usrPath = ConsoleHelper.AskForInfo(String.Concat(message,":"));

                if (!Path.IsPathRooted(usrPath))
                    Console.WriteLine("ERROR: Path is not rooted.");
                
                if (!Directory.Exists(usrPath))
                {
                    //if it doesn't exist, ask if we should create, otherwise ask again
                    if (ConsoleHelper.AskYesNoQuestion("Path does not exist. Create?"))
                    {
                        try
                        {
                            //create path
                            destinationRootPath = Directory.CreateDirectory(usrPath);
                        }
                        catch
                        {
                            //report failure
                            Console.WriteLine("ERROR: Failed attempting to create new path.");
                        }
                    }
                }
                else //exists, so use
                    destinationRootPath = new DirectoryInfo(usrPath);
            } //repeat asking until we have a valid path
            while (destinationRootPath==null || !destinationRootPath.Exists);

            return destinationRootPath;
        }
    }
}
